package com.example.utils;

import java.util.Random;

/**
 * @project: shareCar
 * @description: 随机生成验证码工具类
 * @author: cjq
 * @create: 2022-08-12 14:30
 **/
public class ValidateCodeUtils {
    /**
     * 随机生成验证码
     * @param length 长度为4位或6位
     * @return
     */
    public static Integer generateValidateCode(int length){
        Integer code = null;
        if (length == 4){
            //生成随机数，最大为9999
            code = new Random().nextInt(9999);
            if (code < 1000){
                //保证随机数为4位数字
                code = code + 1000;
            }
        }else if (length == 6){
            //生成随机数，最大为9999
            code = new Random().nextInt(999999);
            if (code < 100000){
                //保证随机数为6位数字
                code = code + 100000;
            }
        }else {
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code;
    }

    /**
     * 随机生成指定长度字符串验证码
     * @param length 长度
     * @return
     */
    public static String generateValidateCode4String(int length){
        Random random = new Random();
        String hexString = Integer.toHexString(random.nextInt());
        String substring = hexString.substring(0, length);
        return substring;
    }
}
